package org.rency.common.trigger.listeners;

import java.util.List;

import javax.annotation.Resource;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.quartz.SchedulerListener;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.rency.common.trigger.beans.TaskStatusEnum;
import org.rency.common.trigger.schedule.repository.DaemonTaskTriggerDO;
import org.rency.common.trigger.schedule.repository.DaemonTaskTriggerRepository;
import org.rency.common.trigger.util.TriggerDict;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component("daemonSchedulerListener")
public class DaemonSchedulerListener implements SchedulerListener{
	
	private static final Logger logger = LoggerFactory.getLogger(DaemonSchedulerListener.class);
	
	@Resource(name="daemonTaskTriggerRepository")
	private DaemonTaskTriggerRepository daemonTaskTriggerRepository;
	
	@Override
	public void jobScheduled(Trigger trigger) {
		logger.debug("队列[{}]中部署新任务[{}].",TriggerDict.DAEMON_TRIGGER_NAME,trigger.getKey());
	}

	@Override
	public void jobUnscheduled(TriggerKey triggerKey) {
		logger.debug("队列[{}]中任务[{}]被移除.",TriggerDict.DAEMON_TRIGGER_NAME,triggerKey.getName());
	}

	@Override
	public void triggerFinalized(Trigger trigger) {
		logger.debug("队列[{}]中的任务[{}]生命周期结束.",TriggerDict.DAEMON_TRIGGER_NAME,trigger.getKey());
	}

	@Override
	public void triggerPaused(TriggerKey triggerKey) {
		logger.debug("队列[{}]中任务[{}]被停止.",TriggerDict.DAEMON_TRIGGER_NAME,triggerKey.getName());
		String beanName = triggerKey.getName().replace(TriggerDict.DAEMON_TRIGGER_NAME, "");
		DaemonTaskTriggerDO entity = daemonTaskTriggerRepository.find(beanName);
		if(TaskStatusEnum.get(entity.getStatus()) != null){
			daemonTaskTriggerRepository.updateStatusByName(beanName, TaskStatusEnum.PAUSE,TaskStatusEnum.get(entity.getStatus()));
		}
	}

	@Override
	public void triggersPaused(String triggerGroup) {
		logger.debug("队列[{}]中属于[{}]组的任务被暂停.",TriggerDict.DAEMON_TRIGGER_NAME,triggerGroup);
	}

	@Override
	public void triggerResumed(TriggerKey triggerKey) {
		logger.debug("队列[{}]中任务[{}]被恢复.",TriggerDict.DAEMON_TRIGGER_NAME,triggerKey.getName());
		String beanName = triggerKey.getName().replace(TriggerDict.DAEMON_TRIGGER_NAME, "");
		DaemonTaskTriggerDO entity = daemonTaskTriggerRepository.find(beanName);
		if(TaskStatusEnum.get(entity.getStatus()) != null){
			daemonTaskTriggerRepository.updateStatusByName(beanName, TaskStatusEnum.NORMAL,TaskStatusEnum.get(entity.getStatus()));
		}		
	}

	@Override
	public void triggersResumed(String triggerGroup) {
		logger.debug("队列[{}]中属于[{}]组的任务被恢复.",TriggerDict.DAEMON_TRIGGER_NAME,triggerGroup);
	}

	@Override
	public void jobAdded(JobDetail jobDetail) {
		logger.debug("队列[{}]中添加新任务[{}].",TriggerDict.DAEMON_TRIGGER_NAME,jobDetail.getKey().getName());
	}

	@Override
	public void jobDeleted(JobKey jobKey) {
		logger.debug("删除队列[{}]中任务[{}].",TriggerDict.DAEMON_TRIGGER_NAME,jobKey.getName());
	}

	@Override
	public void jobPaused(JobKey jobKey) {
		logger.debug("暂停队列[{}]中任务[{}].",TriggerDict.DAEMON_TRIGGER_NAME,jobKey.getName());
	}

	@Override
	public void jobsPaused(String jobGroup) {
		logger.debug("暂停队列[{}]中[{}]组的任务.",TriggerDict.DAEMON_TRIGGER_NAME,jobGroup);
	}

	@Override
	public void jobResumed(JobKey jobKey) {
		logger.debug("队列[{}]中任务[{}]被恢复.",TriggerDict.DAEMON_TRIGGER_NAME,jobKey.getName());
		String beanName = jobKey.getName().replace(TriggerDict.DAEMON_TRIGGER_NAME, "");
		DaemonTaskTriggerDO entity = daemonTaskTriggerRepository.find(beanName);
		if(TaskStatusEnum.get(entity.getStatus()) != null){
			daemonTaskTriggerRepository.updateStatusByName(beanName, TaskStatusEnum.NORMAL,TaskStatusEnum.get(entity.getStatus()));
		}
		logger.debug("队列[{}]中的任务[{}]已恢复.",TriggerDict.DAEMON_TRIGGER_NAME,beanName);
	}

	@Override
	public void jobsResumed(String jobGroup) {
		logger.debug("队列[{}]中组[{}]的任务被恢复.",jobGroup);
		List<DaemonTaskTriggerDO> entityList = daemonTaskTriggerRepository.loadAll();
		for(DaemonTaskTriggerDO entity : entityList){
			if(TaskStatusEnum.get(entity.getStatus()) != null){
				daemonTaskTriggerRepository.updateStatusByName(entity.getBeanName(), TaskStatusEnum.NORMAL,TaskStatusEnum.get(entity.getStatus()));
			}
		}
		logger.debug("队列[{}]中的任务[{}]已恢复.",TriggerDict.DAEMON_TRIGGER_NAME,jobGroup);
	}

	@Override
	public void schedulerError(String msg, SchedulerException cause) {
		logger.error("队列[{}]中发生错误[{}].",TriggerDict.DAEMON_TRIGGER_NAME,msg,cause);
	}

	@Override
	public void schedulerInStandbyMode() {
		
	}

	@Override
	public void schedulerStarted() {
		logger.debug("任务队列[{}]已启动",TriggerDict.DAEMON_TRIGGER_NAME);
	}

	@Override
	public void schedulerStarting() {
		logger.debug("开始启动任务队列[{}]",TriggerDict.DAEMON_TRIGGER_NAME);
	}

	@Override
	public void schedulerShutdown() {
		logger.debug("任务队列[{}]已关闭",TriggerDict.DAEMON_TRIGGER_NAME);
	}

	@Override
	public void schedulerShuttingdown() {
		logger.debug("正在关闭任务队列[{}]",TriggerDict.DAEMON_TRIGGER_NAME);
	}

	@Override
	public void schedulingDataCleared() {
		logger.debug("清空任务队列[{}]所有的数据",TriggerDict.DAEMON_TRIGGER_NAME);
	}

}
